package com.example.softwareconfigurationcoursedesign;

import java.sql.*;
import java.util.ArrayList;
import java.util.Collections;
import java.util.List;

public class ProductService {
    public List<Product> getProductListFromDatabase() {
        // 连接数据库，执行查询操作，获取产品列表数据
        // 假设使用JDBC来访问数据库

        // 假设数据库表名为products，包含id、name、price、imageUrl等字段
        String sql = "SELECT pid,pname,pprice,ptag,pstock,url,id,product_detail FROM products";

        try (Connection connection = DriverManager.getConnection("jdbc:mysql://localhost:3306/course_design", "root", "123456"); Statement statement = connection.createStatement(); ResultSet resultSet = statement.executeQuery(sql)) {

            List<Product> productList = new ArrayList<>();

            while (resultSet.next()) {
                String pid = resultSet.getString("pid");
                String pname = resultSet.getString("pname");
                double pprice = resultSet.getDouble("pprice");
                String ptag = resultSet.getString("ptag");
                String pst = resultSet.getString("pstock");
                String purl = resultSet.getString("url");
                String detail = resultSet.getString("product_detail");
                int id = resultSet.getInt("id");
                Product product = new Product(pid, pname, pprice, ptag, purl, id, detail);
                productList.add(product);
            }

            return productList;

        } catch (SQLException e) {
            e.printStackTrace(System.err);
        }

        return Collections.emptyList();
    }

    public void addProduct(String productName, double productPrice, String productTag, String productDetail) {
        // 连接数据库，执行插入操作，将新商品信息添加到数据库
        // 假设使用JDBC来访问数据库

        String sql = "INSERT INTO products (pname, pprice, ptag, product_detail) VALUES (?, ?, ?, ?)";

        try (Connection connection = DriverManager.getConnection("jdbc:mysql://localhost:3306/course_design", "root", "123456"); PreparedStatement statement = connection.prepareStatement(sql)) {

            statement.setString(1, productName);
            statement.setDouble(2, productPrice);
            statement.setString(3, productTag);
            statement.setString(4, productDetail);

            statement.executeUpdate(); // 执行插入操作
            System.out.println("增加商品");
        } catch (SQLException e) {
            e.printStackTrace(System.err);
        }
    }

    public void editProduct(int productId, String productName, double productPrice, String productTag, String productDetail) {
        // 连接数据库，执行更新操作，将商品信息更新到数据库
        // 假设使用JDBC来访问数据库

        String sql = "UPDATE products SET pname = ?, pprice = ?, ptag = ?, product_detail = ? WHERE id = ?";

        try (Connection connection = DriverManager.getConnection("jdbc:mysql://localhost:3306/course_design", "root", "123456"); PreparedStatement statement = connection.prepareStatement(sql)) {

            statement.setString(1, productName);
            statement.setDouble(2, productPrice);
            statement.setString(3, productTag);
            statement.setString(4, productDetail);
            statement.setInt(5, productId);

            statement.executeUpdate(); // 执行更新操作
            System.out.println("编辑商品");
        } catch (SQLException e) {
            e.printStackTrace(System.err);
        }
    }

    public void deleteProduct(int productId) {
        // 连接数据库，执行删除操作，将指定商品从数据库中删除
        // 假设使用JDBC来访问数据库

        String sql = "DELETE FROM products WHERE id = ?";

        try (Connection connection = DriverManager.getConnection("jdbc:mysql://localhost:3306/course_design", "root", "123456"); PreparedStatement statement = connection.prepareStatement(sql)) {

            statement.setInt(1, productId);

            statement.executeUpdate(); // 执行删除操作
            System.out.println("删除商品");
        } catch (SQLException e) {
            e.printStackTrace(System.err);
        }
    }

    public List<Product> searchProduct(String searchKeyword) {
        // 连接数据库，执行查询操作，根据搜索关键字查找匹配的商品
        // 假设使用JDBC来访问数据库

        String sql = "SELECT * FROM products WHERE pname LIKE ?";

        try (Connection connection = DriverManager.getConnection("jdbc:mysql://localhost:3306/course_design", "root", "123456"); PreparedStatement statement = connection.prepareStatement(sql)) {

            statement.setString(1, "%" + searchKeyword + "%");
            ResultSet resultSet = statement.executeQuery();

            List<Product> searchResults = new ArrayList<>();

            while (resultSet.next()) {
                String pid = resultSet.getString("pid");
                String pname = resultSet.getString("pname");
                double pprice = resultSet.getDouble("pprice");
                String ptag = resultSet.getString("ptag");
                String productDetail = resultSet.getString("product_detail");
                String purl = resultSet.getString("url");
                int id = resultSet.getInt("id");
                Product product = new Product(pid, pname, pprice, ptag, purl, id, productDetail);
                searchResults.add(product);
                System.err.println(pname);
                System.out.println("查找商品");
            }
            return searchResults;
        } catch (SQLException e) {
            e.printStackTrace(System.err);
        }

        return Collections.emptyList();
    }
    public List<Product> getProductListCarFromDatabase(String pname) {
        String sql = "SELECT pid, pprice, ptag, pstock, url, id, product_detail FROM products WHERE pname=?";

        try (Connection connection = DriverManager.getConnection("jdbc:mysql://localhost:3306/course_design", "root", "123456");
             PreparedStatement statement = connection.prepareStatement(sql)) {

            statement.setString(1, pname); // 绑定参数

            try (ResultSet resultSet = statement.executeQuery()) {
                List<Product> productList = new ArrayList<>();

                while (resultSet.next()) {
                    String pid = resultSet.getString("pid");
                    double pprice = resultSet.getDouble("pprice");
                    String ptag = resultSet.getString("ptag");
                    String pst = resultSet.getString("pstock");
                    String purl = resultSet.getString("url");
                    String detail = resultSet.getString("product_detail");
                    int id = resultSet.getInt("id");
                    Product product = new Product(pid, pname, pprice, ptag, purl, id, detail);
                    productList.add(product);
                }

                return productList;
            }
        } catch (SQLException e) {
            e.printStackTrace(System.err);
        }

        return Collections.emptyList();
    }

}

